<template>
<div>
    <div style="width: 600px; margin: 60px auto; padding: 20px 50px;
    position: absolute; left: 30px; top: 20px">
      <h1>欢迎使用</h1><h1>嘉顺惠ERP系统</h1>
      <el-card style="width: 550px;height: 250px;
    margin: 10px auto;background-color: rgba(255,255,255,0.3)">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm"
      style="margin-top: 30px;margin-right: 15px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item style="margin-right: 90px;margin-top: 30px">
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
      </el-card>
    </div>
</div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: 'root',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9081/admins/login';
          console.log('url = ' + url);
          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData = ' + formData);

          this.axios.post(url, formData).then((response) => {
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              this.$message({
                message: '登录成功！',
                type: 'success'
              });
              let jwt = responseBody.data;
              console.log('jwt = ' + jwt);
              localStorage.setItem('jwt',jwt);
              this.$router.push('/');
            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style>
body{
  margin: 0;/*去掉自带的8个像素外边距*/
  background-image: url("imgs/login.jpg");
  background-size: cover; /*设置背景为封面*/
  text-align: center;
}
h1{
  font-size: 64px;
  color: #ffffff;
  margin-bottom: 0;/*去掉下外边距*/
}
img{width: 100px}

h2{
  font-size: 32px;
  color: #0096dc;
  margin-bottom: 0;
}
</style>